package com.lp.mgt.utils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;



/**
 * 权限数据处理
 * 
 * @author lp
 */
public class TreeUtils
{
    /**
     * 根据一个节点id生成一棵树
     * @param list 所有节点列表
     * @param parentId 传入的父节点ID
     * @param field1 父节点字段名如：parentId
     * @param field2 主键字段名如：menuId
     * @param method 设置子节点集合set方法名如：setChildrens
     * 
     */
    public static <T> List<T> getChildPerms(List<T> list, String parentId,String field1,String field2,String method)
    {
        List<T> returnList = new ArrayList<T>();
        for (Iterator<T> iterator = list.iterator(); iterator.hasNext();)
        {
            T t = (T) iterator.next();
            // 一、根据传入的某个父节点ID,遍历该父节点的所有子节点
            Field field;
            String pid=null;
			try {
				field = t.getClass().getDeclaredField(field1);
				field.setAccessible(true);
				pid=(String) field.get(t);
			} catch (NoSuchFieldException e) {
				e.printStackTrace();
			} catch (SecurityException e) {
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
            
            if (pid.equals(parentId)){
                recursionFn(list, t,field1,field2,method);
                returnList.add(t);
            }
        }
        return returnList;
    }

	/**
     * 递归列表
     * @param <T>
     * 
     * @param list
     * @param t
     */
    private static <T> void recursionFn(List<T> list, T t,String field1, String field2, String methodName)
    {
        List<T> childList = getChildList(list, t,field1,field2); // 得到子节点列表
        Method method;
		try {//将得到的儿子节点赋给children字段
			method = t.getClass().getMethod(methodName, List.class);
			method.invoke(t, childList);
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
        for (T tChild : childList)
        {
        	// 判断是否有子节点
            if (getChildList(list, t,field1,field2).size() > 0 ? true : false)
            {
                Iterator<T> it = childList.iterator();
                while (it.hasNext())
                {
                    T n = (T) it.next();
                    recursionFn(list, n,field1,field2,methodName);
                }
            }
        }
    }

    /**
     * 根据某个节点得到子节点列表
     * list:所有节点集合
     * t:待查子节点的节点
     * fieldOne:父节点字段
     * fieldTwo：子节点字段
     * @param <T>
     */
    private static <T> List<T> getChildList(List<T> list, T t,String fieldOne,String fieldTwo)
    {
    	List<T> newList=new ArrayList<>();
    	Field field1;
    	String menuId=null;
		try {
			field1 = t.getClass().getDeclaredField(fieldTwo);
			field1.setAccessible(true);
			menuId=(String) field1.get(t);//父节点
		} catch (NoSuchFieldException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
    	for(T tt:list) {
    		Field field2;
    		String parentId=null;
			try {
				field2 = tt.getClass().getDeclaredField(fieldOne);
				field2.setAccessible(true);
				parentId=(String) field2.get(tt);
			} catch (NoSuchFieldException e) {
				e.printStackTrace();
			} catch (SecurityException e) {
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
        	if(parentId.equals(menuId)) {
        		newList.add(tt);
        	}
    	}
    	return newList;
    }

}
